﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;


namespace lxwmvc
{
    /// <summary>
    /// 分页信息类
    /// </summary>
    public class pagerinfo
    {
        /// <summary>
        /// 默认每页信息为20条
        /// </summary>
        public const int PAGE_SIZE = 20;
        /// <summary>
        /// 得到每页数量
        /// </summary>
        private int _pagesize = PAGE_SIZE;
        /// <summary>
        /// 当前页码
        /// </summary>
        private int _currentpage = 1;

        /// <summary>
        /// 每页记录数量
        /// </summary>
        public int pagesize
        {
            get { return _pagesize; }
            set
            {
                if (value > 0)
                    _pagesize = value;
            }
        }

        /// <summary>
        /// 获取和设置当前第几页
        /// </summary>
        public int currentpage
        {
            get { return _currentpage; }
            set
            {
                if (value <= 0)
                    _currentpage = 1;
                else if (value > pagecount)
                    _currentpage = pagecount;
                else
                    _currentpage = value;
            }
        }

        /// <summary>
        /// 记录前一页
        /// </summary>
        public int pre
        {
            get
            {
                return Math.Max(1, _currentpage - 1);
            }
        }

        /// <summary>
        /// 记录后一页
        /// </summary>
        public int next
        {
            get
            {
                return Math.Min(pagecount, _currentpage + 1);
            }
        }

        /// <summary>
        /// 总共多少页
        /// </summary>
        public int pagecount
        {
            get
            {
                if (recordcount % _pagesize == 0)
                    return recordcount / (int)_pagesize;

                return recordcount / (int)_pagesize + 1;
            }
        }

        /// <summary>
        /// 数据库记录总数
        /// </summary>
        public int recordcount
        {
            get;
            set;
        }

        /// <summary>
        /// 当前页多少条记录，最后一页不一定就是PageSize
        /// </summary>
        public int currentsize
        {
            get
            {
                if (data != null)
                    return data.Count();
                else
                    return 0;
            }
        }

        /// <summary>
        /// 设置第一条记录的索引
        /// </summary>
        /// <param name="intcurpage">当前页</param> 
        /// <param name="intpagesize">每页大小</param>
        public void setfirstindex(int intcurpage, int intpagesize)
        {
            pagesize = intpagesize;
            currentpage = intcurpage;
        }
        /// <summary>
        /// 得到第一条记录的索引
        /// </summary>
        public int firstindex
        {
            get
            {
                return Math.Max(0, (_currentpage - 1) * (int)_pagesize);
            }
        }

        /// <summary>
        /// 得到第一条记录的索引
        /// </summary>
        /// <param name="intcurpage">当前页</param>
        /// <param name="intpagesize">每页大小</param>
        /// <returns></returns>
        internal int getfirstindex(int intcurpage, int intpagesize)
        {
            pagesize = intpagesize;
            currentpage = intcurpage;

            return firstindex;
        }

        IEnumerable<supermodel> _data = null;
        /// <summary>
        /// 获取分页后的数据
        /// </summary>
        public IEnumerable<supermodel> data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }

        /// <summary>
        /// 释放
        /// </summary>
        ~pagerinfo()
        {
            data = null;
        }

        /// <summary>
        /// 显示分页信息
        /// </summary>
        /// <param name="search">搜索的字符串</param>
        /// <returns></returns>
        public string show(string search = "")
        {
            int n = 10;

            string pagerstr = "<font>每页<b>"
                + pagesize + "</b>条</font>/<font>共<b>"
                + recordcount + "</b>条</font>&nbsp;&nbsp;";

            int p;
            if ((currentpage - 1) % n == 0)
                p = (currentpage - 1) / n;
            else
                p = (((currentpage - 1) - (currentpage - 1) % n) / n);

            if (currentpage != 1)
                pagerstr += "<a href='?page=1&"
                    + search + "'><font>首页</font></a> ";
            else
                pagerstr += "<a><font>首页</font></a>";

            if (p * n > 0)
                pagerstr += "<a href='?page=" + (p * n) + "&" + search
                    + "'><font>上一页</font></a> ";
            else
                pagerstr += "<a><font>上一页</font></a>";

            pagerstr += "&nbsp;&nbsp;";

            int i = 0;
            for (i = p * n + 1; i < p * n + n + 1 && pagecount > 0; i++)
            {
                if (i == currentpage)
                    pagerstr += "<font style='color:red'>" + i + "</font> ";
                else
                    pagerstr += "<a href='?page=" + i + "&" + search + "'>" + i + "</a> ";
                if (i == pagecount) break;
            }

            pagerstr += "&nbsp;&nbsp;";

            if (i < pagecount)
                pagerstr += "<a href='?page=" + i + "&"
                    + search + "'><font>后一页</font></a> ";
            else
                pagerstr += "<font>后一页</font> ";

            if (currentpage < pagecount)
                pagerstr += "<a href='?page=" + pagecount + "&"
                    + search + "'><font>末页</font></a> ";
            else
                pagerstr += "<font>末页</font> ";

            pagerstr += "&nbsp;&nbsp;<font>共<b>" + pagecount + "</b>页</font>";

            return pagerstr;
        }
        /// <summary>
        /// 数据json对象
        /// </summary>
        /// <returns></returns>
        public object tojson()
        {
            return new
            {
                pagesize = pagesize,
                currentpage = currentpage,
                pre = pre,
                next = next,
                pagecount = pagecount,
                recordcount = recordcount,
                currentsize = currentsize,
                list = data.tojson()
            };
        }
    }
}